function dataType(obj) {
  if (obj === null) return "Null";
  if (obj === undefined) return "Undefined";
  return Object.prototype.toString.call(obj).slice(8, -1);
};


export function dealElement(obj) {
  let param = {};
  if (obj === null || obj === undefined || obj === "") {
    return param;
  }
  for (let key in obj) {
    if (dataType(obj[key]) === "Object") {
      param[key] = dealElement(obj[key]);
    } else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
      param[key] = obj[key];
    }
  }
  return param;
};

export function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

export function isNumber(value) {
  return typeof value === 'number' && isFinite(value);
}

export function splitObject(arr) {
  if (!Array.isArray(arr)) {
    return null;
  }
  return arr.map(Object.values)
    .reduce((t, arr) => { // transpose the arrays
      arr.forEach((v, i) => (t[i] || (t[i] = [])).push(v));
      return t;
    }, []);
}

export function checkInit() {
  const preKeys = sessionStorage.getItem('preKeys');
  const subKey = sessionStorage.getItem('subKey') || '0';
  const mainKey = sessionStorage.getItem('mainKey') || '0';

  const thisKeys = `${subKey}${mainKey}`;
  if (preKeys === thisKeys && preKeys !== '-1-1') {
    return false;
  } else {
    sessionStorage.setItem('preKeys', thisKeys)
    return true;
  }
}

export function debounce(idle, next, that) {
  let tm = null;
  return function () {
    next.apply(that, arguments);
    clearTimeout(tm);
    // tm = setTimeout(function(){
    //   refreshs.call(that);
    // }, idle);
  }
};

export function unixTimeToDate(timestamp) {
  const date = new Date(timestamp * 1000);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = date.getDate() + ' ';
  const h = date.getHours() + ':';
  const m = date.getMinutes() + ':';
  const s = date.getSeconds();
  return (Y + M + D + h + m + s);
}

export function getBtnList(buttons) {
  let btnList = {};
  if (Array.isArray(buttons)) {
    buttons.forEach(item => {
      btnList[item.name] = item.name
    });
  }

  return btnList
}

const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export function isUrl(path) {
  return reg.test(path);
}

function formData(date) {
  return date < 10 ? '0' + date : date;
};

export function last7Day() {
  var timeArray = new Array();
  var nowtime = Date.parse(String(new Date()));
  for (var i = 0; i < 7; i++) {
    //24*60*60*1000=86400000
    var m = formData(new Date(nowtime - i * 86400000).getMonth() + 1);
    var d = formData(new Date(nowtime - i * 86400000).getDate());
    timeArray.unshift(m + "-" + d)
  };
  
  return timeArray;
}

export function getDaysInOneMonth(year, month) {
  month = parseInt(month, 10);
  var d = new Date(year, month, 0);
  return d.getDate();
}

export function getFileType(url = "") {
  const fileList = ['png', 'jpg', 'jpeg', 'xlsx', 'xls', 'docx', 'doc', 'pdf'];
  const suffix = url.substr(url.lastIndexOf('.') + 1).toLowerCase();

  const ind = fileList.indexOf(suffix)

  switch (ind) {
    case 7:
      return 'pdf';
    case 6:
    case 5:
    case 4:
    case 3:
      return 'office';
    case 2:
    case 1:
    case 0:
      return 'image';
    default:
      return null;
  }
}

export function firstUpperCase(str) {
  return str.replace(/^\S/g,function(s){return s.toUpperCase();});
}